/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * mainMenu.java
 *
 * Created on Oct 10, 2010, 12:54:01 AM
 */

package restaurantmanagementsystem;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JFrame;

/**
 *
 * @author Ed
 */
public class mainMenu extends javax.swing.JFrame {

    newAccount newAccountWindow;
    Connection con;

    /** Creates new form mainMenu */
    public mainMenu() {
        super("Restaurant Management System");
        initComponents();
        initDatabaseConnection();

        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension windowSize = this.getSize();

        int windowX = Math.max(0, (screenSize.width  - windowSize.width ) / 2);
        int windowY = Math.max(0, (screenSize.height - windowSize.height) / 2);

        this.setLocation(windowX, windowY);
        this.setSize(windowSize);
        this.setResizable(false);
        logInPanel.setVisible(true);
        warningLabel.setVisible(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        burgerIcon = new javax.swing.JLabel();
        labelLogo1 = new javax.swing.JLabel();
        labelLogo2 = new javax.swing.JLabel();
        logInPanel = new javax.swing.JPanel();
        labelUsername = new javax.swing.JLabel();
        usernameField = new javax.swing.JTextField();
        labelWelcome = new javax.swing.JLabel();
        labelPassword = new javax.swing.JLabel();
        passwordField = new javax.swing.JPasswordField();
        loginButton = new javax.swing.JLabel();
        newUserButton = new javax.swing.JLabel();
        exitButton = new javax.swing.JLabel();
        iconLabel = new javax.swing.JLabel();
        warningLabel = new javax.swing.JLabel();
        background1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        burgerIcon.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/burger icon.png"))); // NOI18N
        jPanel1.add(burgerIcon, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 70, -1, -1));

        labelLogo1.setFont(new java.awt.Font("DejaVu Sans", 1, 16));
        labelLogo1.setForeground(new java.awt.Color(90, 90, 90));
        labelLogo1.setText("Restaurant Management");
        jPanel1.add(labelLogo1, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 200, 230, 40));

        labelLogo2.setFont(new java.awt.Font("DejaVu Sans", 1, 16));
        labelLogo2.setForeground(new java.awt.Color(90, 90, 90));
        labelLogo2.setText("System");
        jPanel1.add(labelLogo2, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 240, 70, 40));

        logInPanel.setOpaque(false);
        logInPanel.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        labelUsername.setFont(new java.awt.Font("Tahoma", 1, 11));
        labelUsername.setText("USERNAME  :");
        logInPanel.add(labelUsername, new org.netbeans.lib.awtextra.AbsoluteConstraints(27, 116, -1, 20));

        usernameField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                usernameFieldKeyPressed(evt);
            }
        });
        logInPanel.add(usernameField, new org.netbeans.lib.awtextra.AbsoluteConstraints(117, 116, 120, 20));

        labelWelcome.setFont(new java.awt.Font("Tahoma", 1, 16));
        labelWelcome.setText("WELCOME");
        logInPanel.add(labelWelcome, new org.netbeans.lib.awtextra.AbsoluteConstraints(87, 76, -1, -1));

        labelPassword.setFont(new java.awt.Font("Tahoma", 1, 11));
        labelPassword.setText("PASSWORD :");
        logInPanel.add(labelPassword, new org.netbeans.lib.awtextra.AbsoluteConstraints(27, 156, -1, 20));

        passwordField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                passwordFieldKeyPressed(evt);
            }
        });
        logInPanel.add(passwordField, new org.netbeans.lib.awtextra.AbsoluteConstraints(117, 156, 120, 20));

        loginButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/login.png"))); // NOI18N
        loginButton.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        loginButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                loginButtonMouseClicked(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                loginButtonMouseExited(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                loginButtonMouseEntered(evt);
            }
        });
        logInPanel.add(loginButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(77, 208, -1, -1));

        newUserButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/newuser.png"))); // NOI18N
        newUserButton.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        newUserButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                newUserButtonMouseClicked(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                newUserButtonMouseExited(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                newUserButtonMouseEntered(evt);
            }
        });
        logInPanel.add(newUserButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(117, 208, 30, -1));

        exitButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/exit.png"))); // NOI18N
        exitButton.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        exitButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                exitButtonMouseClicked(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                exitButtonMouseExited(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                exitButtonMouseEntered(evt);
            }
        });
        logInPanel.add(exitButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(167, 208, -1, -1));

        iconLabel.setFont(new java.awt.Font("Tahoma", 1, 12));
        iconLabel.setText(" ");
        logInPanel.add(iconLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(101, 239, 70, 20));

        warningLabel.setFont(new java.awt.Font("Ubuntu", 0, 12));
        warningLabel.setForeground(new java.awt.Color(170, 7, 35));
        warningLabel.setText("*Invalid username/password");
        logInPanel.add(warningLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(57, 179, 160, 20));

        jPanel1.add(logInPanel, new org.netbeans.lib.awtextra.AbsoluteConstraints(270, 0, 260, 330));

        background1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/background.png"))); // NOI18N
        jPanel1.add(background1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 530, 330));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void loginButtonMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_loginButtonMouseEntered
        iconLabel.setText(" Log In");
}//GEN-LAST:event_loginButtonMouseEntered

    private void loginButtonMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_loginButtonMouseExited
        iconLabel.setText(" ");
}//GEN-LAST:event_loginButtonMouseExited

    private void newUserButtonMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_newUserButtonMouseEntered
        iconLabel.setText("New User");
}//GEN-LAST:event_newUserButtonMouseEntered

    private void newUserButtonMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_newUserButtonMouseExited
        iconLabel.setText(" ");
}//GEN-LAST:event_newUserButtonMouseExited

    private void exitButtonMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_exitButtonMouseEntered
        iconLabel.setText("   Exit");
}//GEN-LAST:event_exitButtonMouseEntered

    private void exitButtonMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_exitButtonMouseExited
        iconLabel.setText(" ");
}//GEN-LAST:event_exitButtonMouseExited

    private void loginButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_loginButtonMouseClicked
        userVerification();
    }//GEN-LAST:event_loginButtonMouseClicked

    private void usernameFieldKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_usernameFieldKeyPressed
        int key = evt.getKeyCode();
        if (key == KeyEvent.VK_ENTER) {
            userVerification();
        }
    }//GEN-LAST:event_usernameFieldKeyPressed

    private void passwordFieldKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_passwordFieldKeyPressed
        int key = evt.getKeyCode();
        if (key == KeyEvent.VK_ENTER) {
            userVerification();
        }
    }//GEN-LAST:event_passwordFieldKeyPressed

    private void exitButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_exitButtonMouseClicked
        dispose();
    }//GEN-LAST:event_exitButtonMouseClicked

    private void newUserButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_newUserButtonMouseClicked
        if (newAccountWindow == null){
            newAccountWindow = new newAccount();
            newAccountWindow.show();
            newAccountWindow.addWindowListener(new FrameDrawListener());
            newAccountWindow.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        }
    }//GEN-LAST:event_newUserButtonMouseClicked

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new mainMenu().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel background1;
    private javax.swing.JLabel burgerIcon;
    private javax.swing.JLabel exitButton;
    private javax.swing.JLabel iconLabel;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel labelLogo1;
    private javax.swing.JLabel labelLogo2;
    private javax.swing.JLabel labelPassword;
    private javax.swing.JLabel labelUsername;
    private javax.swing.JLabel labelWelcome;
    private javax.swing.JPanel logInPanel;
    private javax.swing.JLabel loginButton;
    private javax.swing.JLabel newUserButton;
    private javax.swing.JPasswordField passwordField;
    private javax.swing.JTextField usernameField;
    private javax.swing.JLabel warningLabel;
    // End of variables declaration//GEN-END:variables

    public void initDatabaseConnection(){
        
        try {
            DriverManager.registerDriver (new oracle.jdbc.OracleDriver());

            con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "edwind", "cat200");
            System.out.println("Database Connected");
        }
        catch (Exception e){
            System.out.println("Error");
        }
    }

    public void userVerification(){

        String tempUsername = usernameField.getText();
        String tempPassword = passwordField.getText();

        boolean authentication = false;

        try {

            Statement statement = con.createStatement();
            ResultSet rset = statement.executeQuery("Select USERNAME From USERSINFO");

            while (rset.next()){

                if (tempUsername.equals(rset.getString("USERNAME")))//comparing to usernames in database
                {
                    rset = statement.executeQuery("Select PASSWORD From USERSINFO");
                        while (rset.next()){
                            if (tempPassword.equals(rset.getString("PASSWORD"))){//comparing to passwords in database
                                authentication = true;
                                warningLabel.setVisible(false);
                            }
                        }
                }
            }
        }
        catch (Exception e){
            System.out.println("Error");
        }


        if (authentication){

            DateFormat date_Format = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
            Date date = new Date();

            String date2 = "";
            String date3 = "";
            String time = "";

            for (int i = 0; i < 3; i++){

                date2 = date2 + (date_Format.format(date)).charAt(i);

            }

            for (int i = 3; i < 5; i++){

                date3 = date3 + (date_Format.format(date)).charAt(i);

            }

            if (date3.equals("01")){
                date3 = "JAN";
            }
            else if (date3.equals("02")){
                date3 = "FEB";
            }
            else if (date3.equals("03")){
                date3 = "MAR";
            }
            else if (date3.equals("04")){
                date3 = "APR";
            }
            else if (date3.equals("05")){
                date3 = "MAY";
            }
            else if (date3.equals("06")){
                date3 = "JUN";
            }
            else if (date3.equals("07")){
                date3 = "JUL";
            }
            else if (date3.equals("08")){
                date3 = "AUG";
            }
            else if (date3.equals("09")){
                date3 = "SEP";
            }
            else if (date3.equals("10")){
                date3 = "OCT";
            }
            else if (date3.equals("11")){
                date3 = "NOV";
            }
            else if (date3.equals("12")){
                date3 = "DEC";
            }

            date2 = date2 + date3;

            for (int i = 5; i < 10; i++){

                date2 = date2 + (date_Format.format(date)).charAt(i);

            }

            for (int i = 11; i < (date_Format.format(date)).length(); i++){

                time = time + (date_Format.format(date)).charAt(i);
            }


            try {

                Statement statement = con.createStatement();
                String updateString1;

                updateString1 = "UPDATE USERSINFO set LAST_LOGIN_DATE = '" + date2 + "', "
                        + "LAST_LOGIN_TIME = '" + time + "'"
                        + " WHERE USERNAME = '" + tempUsername + "'";
                System.out.println(updateString1);

                statement.executeUpdate(updateString1);//saving the last login date and time into database

                statement.close();
            }
            catch (Exception e){
                System.out.println("error");
            }
            createNewWindow();
        }

        else{
            warningLabel.setVisible(true);
        }
    }
    
    public void destroy() {               //when the login menu is closed,
      if (newAccountWindow != null) {     //the new account window closes
          newAccountWindow.dispose();
          newAccountWindow = null;
       }
   }

   class FrameDrawListener extends WindowAdapter {
      synchronized public void windowClosed(WindowEvent evt) {
         newAccountWindow = null;
      }
   }

   financeAndAdmin windowFinanceAndAdmin;
   humanResourceModule windowHumanResourceModule;
   salesAndMarketingModule windowSalesAndMarketingModule;

   public void createNewWindow(){

       int privilege = 0;
       try {
           Statement statement = con.createStatement();
           String query = "SELECT PRIVILEGE FROM USERSINFO WHERE USERNAME = '" + usernameField.getText() + "'";
           System.out.println(query);
           ResultSet rset = statement.executeQuery(query);

           while (rset.next()){
               privilege = rset.getInt("PRIVILEGE");
           }

           if (privilege == 1){
               windowFinanceAndAdmin = new financeAndAdmin();
               windowFinanceAndAdmin.show();
           }
           else if (privilege == 2){
               windowHumanResourceModule = new humanResourceModule();
               windowHumanResourceModule.show();
           }
           else if (privilege == 3){
               windowSalesAndMarketingModule = new salesAndMarketingModule();
               windowSalesAndMarketingModule.show();

           }

           statement.close();
           con.close();
           usernameField.setText("");
           passwordField.setText("");
           this.dispose();

       }
       catch(Exception e){
           System.out.println("Error connecting to Database");
       }
   }
}
